using System;
using System.Collections.Generic;
using System.Text;

namespace SymAutomate.HostTelnetConnection
{
    /// <summary>
    /// Error Numbers 200-219; Reference file ErrorNumbers.txt for more information.
    /// 200 Error: Required argument 'hostip' missing. Use /h switch for help.
    /// 201 Error: Required argument 'aixusername' missing. Use /h switch for help.
    /// 202 Error: Required argument 'aixpassword' missing. Use /h switch for help.
    /// 203 Error: Required argument 'sym' missing. Use /h switch for help.
    /// 204 Error: Required argument 'symuserid' missing. Use /h switch for help.
    /// 205 Error: Required argument 'telnetcommandstimeoutinsec' missing or not greater than 0. Use /h switch for help.
    /// 206 Error: Required argument 'command' missing. Use /h switch for help.
    /// 207 Error: Invaid value for 'autorenewpassword'. %ExceptionMessage%.
    /// 208 Error: Invaid value for 'debug'. %ExceptionMessage%.
    /// 209 Error: Encryption is enabled but Encryption Key is not set. Run Command=GenerateEncryptionKey.
    /// 210 Error: Encrypted 'aixpassword' from the config file could not be decrypted.
    /// 211 Error: Encrypted 'symopoperatorid' from the config file could not be decrypted.
    /// 212 Error: Encrypted 'symuserid' from the config file could not be decrypted. 
    /// 213 Error: Required argument 'symopoperatorid' missing. Use /h swith for help.
    /// </summary>
    class Parameters
    {
        #region Class Variables and Access Methods
        private bool __EncryptSensitiveKeys;
        private string __EncryptionKey;
        private string __HostIp;
        private string __AixUsername;
        private string __AixPassword;
        private string __SymopOperatorId;
        private string __SymUserId;
        private string __Sym;
        private bool __AutoRenewPassword;
        private int __TelnetCommandsTimeoutInSec;
        private string __Command;
        private bool __Debug;

        public bool EncryptSensitiveKeys { get { return __EncryptSensitiveKeys; } }
        public string EncryptionKey { get { return __EncryptionKey; } }
        public string HostIp { get { return __HostIp; } }
        public string AixUsername { get { return __AixUsername; } }
        public string AixPassword { get { return __AixPassword; } }
        public string SymUserId { get { return __SymUserId; } }
        public string SymopOperatorId { get { return __SymopOperatorId; } }
        public string Sym { get { return __Sym; } }
        public bool AutoRenewPassword { get { return __AutoRenewPassword; } }
        public int TelnetCommandsTimeoutInSec { get { return __TelnetCommandsTimeoutInSec; } }
        public string Command { get { return __Command; } }
        public bool Debug { get { return __Debug; } }
        #endregion

        public Parameters(ConsoleArguments _ConsoleArguments)
        {
            //defaults
            __EncryptSensitiveKeys = false;
            __Debug = false;
            __AutoRenewPassword = false;

            //read from config file
            __EncryptSensitiveKeys = System.Configuration.ConfigurationManager.AppSettings["encryptsensitivekeys"] == "true" ? true : false;
            __EncryptionKey = Util.Driver.ReadRegistryKey(@"HKEY_LOCAL_MACHINE\Software\SymAutomate", "EncryptionKey");
            __HostIp = System.Configuration.ConfigurationManager.AppSettings["hostip"];
            __AixUsername = System.Configuration.ConfigurationManager.AppSettings["aixusername"];
            __AixPassword = System.Configuration.ConfigurationManager.AppSettings["aixpassword"];
            __SymopOperatorId = System.Configuration.ConfigurationManager.AppSettings["symopoperatorid"];
            __SymUserId = System.Configuration.ConfigurationManager.AppSettings["symuserid"];       
            __Sym = System.Configuration.ConfigurationManager.AppSettings["sym"];
            __AutoRenewPassword = System.Configuration.ConfigurationManager.AppSettings["autorenewpassword"] == "true" ? true : false;

            __TelnetCommandsTimeoutInSec = GlobalFunctions.GetInt(System.Configuration.ConfigurationManager.AppSettings["telnetcommandstimeoutinsec"]).HasValue ? (int)GlobalFunctions.GetInt(System.Configuration.ConfigurationManager.AppSettings["telnetcommandstimeoutinsec"]) : 3;
            __Debug = System.Configuration.ConfigurationManager.AppSettings["debug"] == "true" ? true : false;

            #region Decrypt the values if encryption is enabled
            if (__EncryptSensitiveKeys && !string.IsNullOrEmpty(__EncryptionKey))
            {
                AESEncryption _AESEncryption = new AESEncryption(__EncryptionKey);
                try
                {
                    __AixPassword = _AESEncryption.Decrypt(__AixPassword);
                }
                catch (Exception _DecyptionException)
                {
                    throw new ReturnException("210 Error: Encrypted 'aixpassword' from the config file could not be decrypted.");
                }
                try
                {
                    __SymopOperatorId = _AESEncryption.Decrypt(__SymopOperatorId);
                }
                catch (Exception _DecyptionException)
                {
                    throw new ReturnException("211 Error: Encrypted 'symopoperatorid' from the config file could not be decrypted.");
                }
                try
                {
                    __SymUserId = _AESEncryption.Decrypt(__SymUserId);
                }
                catch (Exception _DecyptionException)
                {
                    throw new ReturnException("212 Error: Encrypted 'symuserid' from the config file could not be decrypted.");
                }
            } 
            #endregion

            //save from command line arguments
            if (_ConsoleArguments.ArgumentCollection.Contains("hostip")) __HostIp = _ConsoleArguments.ArgumentCollection["hostip"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("aixusername")) __AixUsername = _ConsoleArguments.ArgumentCollection["aixusername"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("aixpassword")) __AixPassword = _ConsoleArguments.ArgumentCollection["aixpassword"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("symopoperatorid")) __SymopOperatorId = _ConsoleArguments.ArgumentCollection["symopoperatorid"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("symuserid")) __SymUserId = _ConsoleArguments.ArgumentCollection["symuserid"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("sym")) __Sym = _ConsoleArguments.ArgumentCollection["sym"].ToString();
            try
            {
                if (_ConsoleArguments.ArgumentCollection.Contains("autorenewpassword")) __AutoRenewPassword = GlobalFunctions.GetBooleanValue(_ConsoleArguments.ArgumentCollection["autorenewpassword"].ToString());
            }
            catch (ArgumentException _ArgumentException)
            {
                throw new ReturnException("207 Error: Invaid value for 'autorenewpassword'. " + _ArgumentException.Message + ".");
            }
            if (_ConsoleArguments.ArgumentCollection.Contains("command")) __Command = _ConsoleArguments.ArgumentCollection["command"].ToString();
            try
            {
                if (_ConsoleArguments.ArgumentCollection.Contains("debug")) __Debug = GlobalFunctions.GetBooleanValue(_ConsoleArguments.ArgumentCollection["debug"].ToString());
            }
            catch (ArgumentException _ArgumentException)
            {
                throw new ReturnException("208 Error: Invaid value for 'debug'. " + _ArgumentException.Message + ".");
            }

            //perform argument error checking
            if (string.IsNullOrEmpty(__HostIp))
                throw new ReturnException("200 Error: Required argument 'hostip' missing. Use /h switch for help.");
            if (string.IsNullOrEmpty(__AixUsername))
                throw new ReturnException("201 Error: Required argument 'aixusername' missing. Use /h switch for help.");
            if (string.IsNullOrEmpty(__AixPassword))
                throw new ReturnException("202 Error: Required argument 'aixpassword' missing. Use /h switch for help.");
            if (string.IsNullOrEmpty(__SymopOperatorId))
                throw new ReturnException("213 Error: Required argument 'symopoperatorid' missing. Use /h swith for help.");
            if (string.IsNullOrEmpty(__Sym) || !GlobalFunctions.GetInt(__Sym).HasValue)
                throw new ReturnException("203 Error: Required argument 'sym' missing or invalid. Use /h switch for help.");
            if (string.IsNullOrEmpty(__SymUserId))
                throw new ReturnException("204 Error: Required argument 'symuserid' missing. Use /h switch for help.");
            if (__TelnetCommandsTimeoutInSec == 0)
                throw new ReturnException("205 Error: Required argument 'telnetcommandstimeoutinsec' missing or not greater than 0. Use /h switch for help.");
            if (string.IsNullOrEmpty(__Command))
                throw new ReturnException("206 Error: Required argument 'command' missing. Use /h switch for help.");

            if (__EncryptSensitiveKeys && string.IsNullOrEmpty(__EncryptionKey))
                throw new ReturnException("209 Error: Encryption is enabled but Encryption Key is not set. Run Command=GenerateEncryptionKey.");
        }
    }
}
